package dkm;

import static org.junit.Assert.assertTrue;
import dkm.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * This JUnit Test ensures that PersonalWall is moving along the walls of the battlefield.
 * It does this by checking to make sure that PersonalWalls is moving close to the walls.
 * @author David Mau
 */
public class TestGunPositionStrategy extends RobotTestBed {

  private double perpendicularGunTurns = 0.0;
  private double totalTurns = 0.0;
  
  /**
   * Specifies that SittingDuck and PersonalWall are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,dkm.PersonalWall";
  }
  
  /**
   * This test runs for 10 rounds.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Count how many turns PersonalWall's gun stays perpendicular to the way it is moving.
   * @param event information stored about the robots at the end of a turn
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double gunHeading = robot.getGunHeading();
    double robotHeading = robot.getBodyHeading();
    
    // Add to the turn counter if the gun is about perpendicular to the robot's heading.
    if ((gunHeading > (robotHeading + (Math.PI / 2) - 0.1)) &&
        (gunHeading < (robotHeading + (Math.PI / 2) + 0.1 )) ) {
      perpendicularGunTurns++;
    }
    else if ((gunHeading > (robotHeading + (3 * Math.PI / 2) - 0.1)) &&
        (gunHeading < (robotHeading + (3 * Math.PI / 2) + 0.1 ))) {
      perpendicularGunTurns++;
    }
    
    // Increment the number of turns that have gone by.
    totalTurns++;
  }

  /**
   * The actual test, which asserts that PersonalWalls has run along the walls.
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue( "Robot has kept it's gun perpendicular most of the time.",
        ((perpendicularGunTurns / totalTurns) >= 0.5) );
  }
}